app.controller('RtOrderDetailCtrl', function ($scope, $stateParams, ajax, toaster, $modal) {

	$scope.status = $stateParams.status;
	$scope.orderId = $stateParams.orderId;
	$scope.tag = $stateParams.tag;
	
	$scope.hide = false;
	if ($scope.status == 3) {
		$scope.hide = true;
	}
	if ($scope.status == 9) {
		$scope.sd = true;
	}

	//查询
	$scope.reload = function () {
		ajax.call(new Req.GET({
			resource: 'order/' + 'rt/' + $stateParams.orderId + '/id'
		})).then(function (result) {
			$scope.order = result.result;
		}, function () { });
	};

	//查询订单支付方式
	$scope.reloadPay = function () {
		ajax.call(new Req.GET({
			resource: 'order/' + $stateParams.orderId + '/payname'
		})).then(function (result) {
			$scope.payname = result.result;
		}, function () { });
	};

	$scope.reloadPay();
	if ($scope.orderId) {
		$scope.reload();
	}

	$scope.ids = [];
	$scope.checks = false;//默认未选中
	$scope.ckAll = false;//默认未选中
	$scope.choseArr = [];
	$scope.flag = false;

	/*单选*/
	$scope.check = function (val) {
		if ($scope.ids.indexOf(val) < 0) {
			$scope.ids.push(val);
			if ($scope.ids != undefined && $scope.ids != null && $scope.ids != "") {
				$scope.flag = true;
			}
			if ($scope.ids != undefined && $scope.ids != null && $scope.ids != "") {
				$scope.flag = true;
			} else {
				$scope.flag = false;
			}
		} else {
			$scope.ids.splice($scope.ids.indexOf(val), 1);
			if ($scope.ids != undefined && $scope.ids != null && $scope.ids != "") {
				$scope.flag = true;
			}
			if ($scope.ids != undefined && $scope.ids != null && $scope.ids != "") {
				$scope.flag = true;
			} else {
				$scope.flag = false;
			}
		}
	};

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.count = 0;
	$scope.ok = function (order) {
		inserter.datalist = $scope.ids;
		inserter.resource = 'order/rt/' + order.passengerId + '/' + order.id + '/prefund';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				$scope.prefund = result.result;
				if ($scope.prefund.success) {
					var message = $scope.prefund.message;
					$scope.json = angular.fromJson(message);
					var modalInstance = $modal.open({
						templateUrl: 'template/rent/order/module.html?v=452123d2d9',
						controller: 'RentOrderModalCtrl',
						size: 'sm',
						resolve: {
							param: function () {
								return {
									"type": "refund",
									"charge": $scope.json.charge,
									"refund": $scope.json.refund
								};
							}
						}
					});

					modalInstance.result.then(function () {
						inserter.resource = 'order/rt/' + $scope.ids + '/' + order.id + '/refund';
						ajax.call(inserter).then(function (result) {
							if (result.code == 200) {
								if (result.result.success) {
									$scope.flag = false;
									$scope.reload();
									toaster.pop('success', '成功', '申请退票成功。');
								} else {
									toaster.pop('error', '错误', result.result.message);
								}
							} else {
								toaster.pop('error', '错误', '申请退票失败！');
							}
						}, function () {
							toaster.pop('error', '错误', '申请退票失败！');
						});
					});
				} else {
					toaster.pop('info', '提示', $scope.prefund.message);
				}
			}
		}, function () {
			toaster.pop('error', '错误', '申请退票失败！');
		});
	};

	$scope.refund = function (order) {
		if ($scope.ids[0] != null) {
			inserter.datalist = $scope.ids;
			inserter.resource = 'order/rt/' + order.passengerId + '/' + order.id + '/rtrefund';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					$scope.prefund = result.result;
					if ($scope.prefund.success) {
						var message = $scope.prefund.message;
						$scope.json = angular.fromJson(message);
						var modalInstance = $modal.open({
							templateUrl: 'template/rent/order/module.html?v=452123d2d9',
							controller: 'RentOrderModalCtrl',
							resolve: {
								param: function () {
									return {
										"type": "update",
										"charge": $scope.json.charge,
										"refund": $scope.json.refund
									};
								}
							}
						});

						modalInstance.result.then(function (param) {
							inserter.fields = param;
							inserter.resource = 'order/rt/' + order.id + '/' + $scope.ids + '/update';
							ajax.call(inserter).then(function (result) {
								if (result.code == 200) {
									$scope.flag = false;
									$scope.reload();
									$scope.ids = [];
									toaster.pop('success', '成功', '手动退票成功。');

								} else {
									toaster.pop('error', '错误', result.result);
								}
							}, function () {
								toaster.pop('error', '错误', '手动退票失败！');
							});
						});
					} else {
						toaster.pop('info', '提示', $scope.prefund.message);
					}
				}
			}, function () {
				toaster.pop('error', '错误', '申请退票失败！');
			});
		} else {
			toaster.pop('warning', '提示', '请选择车票！');
		}
	};
});